import { defineStore } from 'pinia'
import { ref } from 'vue'
import { type cart } from '@/typings'

export const SubmitOrder = defineStore('order', () => {
    const totalOrder = ref<Array<cart>>([])
    const orderObj = ref<Array<cart>>([])
    const payLoding = ref<Array<cart>>([])
    const overPay = ref<Array<cart>>([])
    const submit = (obj: Array<cart>) => {
        orderObj.value = [...obj]
        
    }
    const cancelPay = () => {

        payLoding.value.push(...orderObj.value);
       
    }
    const overBtn = () => {
        
        overPay.value.push(...orderObj.value);
       
        

        
    }
    const setTotalOrder = () => {
        let data = payLoding.value.concat(overPay.value)
        totalOrder.value = data
    }

    const deleteOrder = (item: Array<cart>) => {
        hhh(item)
    }
    const returnOrder = (item: Array<cart>) => {
        hhh(item)

    }
    const cancelOrder = (item: Array<cart>) => {
        hhh(item)
    }
    const hhh = (item: Array<cart>) => {
        let index = -1
        for (let i = 0; i < payLoding.value.length; i++) {
            for (let j = 0; j < item.length; j++) {
                if (payLoding.value[i].name == item[j].name && payLoding.value[i].color == item[j].color && payLoding.value[i].level == item[j].level) {
                    index = i
                }
            }
        }
        if (index !== -1) {
            payLoding.value.splice(index, 1)
            return
        }
        for (let i = 0; i < overPay.value.length; i++) {
            for (let j = 0; j < item.length; j++) {
                if (overPay.value[i].name == item[j].name && overPay.value[i].color == item[j].color && overPay.value[i].level == item[j].level) {
                    index = i
                }
            }
        }
        if (index !== -1) {
            overPay.value.splice(index, 1)
        }
    }



    return {
        orderObj,
        submit,
        payLoding,
        cancelPay,
        overPay,
        overBtn,
        totalOrder,
        setTotalOrder,
        deleteOrder,
        returnOrder,
        cancelOrder,


    }
}, {
    persist: true,
})